package cc.miwuya.property.controller;

import cc.miwuya.common.util.Result;
import cc.miwuya.core.BaseController;
import cc.miwuya.property.domain.Shops;
import cc.miwuya.property.service.ShopsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @Author 迷雾森林
 * @Date 2024-02-27 14:54
 * @Desc 商户控制器
 */
@RestController
@RequestMapping("/property/shops")
public class ShopsController extends BaseController {
    @Resource
    ShopsService shopsService;


    @PostMapping("/edit")
    public Result edit(@RequestBody Shops shops) {
        shops.setUpdated(LocalDateTime.now());
        System.out.println(shops);
        shopsService.saveOrUpdate(shops);
        return Result.success();
    }

    @GetMapping("/info/{shopsId}")
    public Result info(@PathVariable Long shopsId) {
        return Result.success(shopsService.getById(shopsId));
    }

    /**
     * 根据小区ID获取商铺
     */
    @GetMapping("/list/{residentialId}")
    public Result list(@PathVariable Long residentialId) {
        return Result.success(shopsService.list(new QueryWrapper<Shops>().eq("residential_id", residentialId)));
    }

    /**
     * 获取所有商铺
     */
    @GetMapping("/list")
    public Result list(String name) {
        return Result.success(shopsService.getAllShops(getPage(),name));
    }

    /**
     * 获取业主名下所有商铺
     */
    @GetMapping("/ownerShopsList/{ownerId}")
    public Result getOwnerShopsList(@PathVariable Long ownerId,String name) {
        Object ownerList = shopsService.getOwnerList(getPage(), ownerId, name);
        return Result.success(ownerList);
    }

    /**
     * 回收出租的商铺
     */
    @GetMapping("/recover")
    public Result recover(Long shopsId){
        Shops id = shopsService.getById(shopsId);
        Shops shops = new Shops();
        shops.setAddress(id.getAddress());
        shops.setArea(id.getArea());
        shops.setId(id.getId());
        shops.setUpdated(LocalDateTime.now());
        shops.setResidentialId(id.getResidentialId());
        shopsService.removeById(id);
        boolean save = shopsService.save(shops);
        System.out.println(save);
        return Result.success();
    }
    @PostMapping("/editOwnerShops")
    public Result editShops(@RequestBody Shops shops) {
        shops.setUpdated(LocalDateTime.now());
        shopsService.saveOrUpdate(shops);
        return Result.success();
    }


}
